package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.xumt.hwHMSdemo.cannectSb.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderActivity extends AppCompatActivity {
    private SimpleAdapter orderadapter = null;
//    private String orderlistresult;
    public static int clickoderid=0;
    private  int state=0;
    public static String address;
    private List<Map<String, Object>> orderlist = new ArrayList<Map<String, Object>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        orderadapter = new OrderActivity.MySimpleAdapter(this, getData(), R.layout.orderlist, new String[]{"username", "price", "id"},
                new int[]{R.id.text1, R.id.orderprice, R.id.orderid});
        ListView listView = (ListView) findViewById(R.id.orderlist);
        listView.setAdapter(orderadapter);
        AdapterView.OnItemClickListener itemClickListener=new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object>map=orderlist.get(position);
                clickoderid=(int) map.get("id1");
                state=(int) map.get("state");
                address=(String)map.get("address");
                if (state==0){
                    Intent intent=new Intent(OrderActivity.this,NopayActivity.class);
                    startActivity(intent);
                }
                else if(state==1){
                    Intent intent=new Intent(OrderActivity.this,NosendActivity.class);
                    startActivity(intent);
                }
                else if(state==3){
                    Intent intent=new Intent(OrderActivity.this,PaidActivity.class);
                    startActivity(intent);
                }
                else if(state==4){
                    Intent intent=new Intent(OrderActivity.this,FinishedActivity.class);
                    startActivity(intent);
                }
            }
        };
        listView.setOnItemClickListener(itemClickListener);
    }
    //通过state查找订单
//    private void findstate() {
//        new Thread() {
//            public void run() {
//                try {
//                    HttpRequest request = new HttpRequest();
//                   orderlistresult = request.sendGet("http://169.254.105.90:8081/Order/state","state=0");
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();
//
//    }
    //获取数据
    private List<Map<String, Object>> getData() {
        try {
//            findstate();
            Thread.sleep(500);
            JSONArray jsonArray = new JSONArray(OrderManagementActivity.orderlistresult);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("username", "购买用户为：" + jsonObject.getString("name"));
                map.put("price", "价格：" + jsonObject.getString("price"));
                map.put("id", "订单号：" + jsonObject.getString("orderid"));
                map.put("id1", jsonObject.getInt("orderid"));
                map.put("state",jsonObject.getInt("state"));
                map.put("address",jsonObject.getString("address"));
               orderlist.add(map);
            }
        } catch (JSONException | InterruptedException e) {
            e.printStackTrace();
        }
        return orderlist;
    }
    private class MySimpleAdapter extends SimpleAdapter {

        public MySimpleAdapter(Context context,
                               List<? extends Map<String, ?>> data, int resource,
                               String[] from, int[] to) {
            super(context, data, resource, from, to);
            // TODO Auto-generated constructor stub
        }

    }
    }
